package com.sc.nft.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.MallOrderStatusLogDao;
import com.sc.nft.entity.MallOrder;
import com.sc.nft.entity.MallOrderStatusLog;
import com.sc.nft.entity.dto.BatchDeliverDTO;
import com.sc.nft.entity.vo.DistributionOrderVO;
import com.sc.nft.entity.vo.ExportOrderListVO;
import com.sc.nft.entity.vo.mall.AdminMallPageOrderVO;
import com.sc.nft.entity.vo.mall.AdminOrderDetailsVO;
import com.sc.nft.entity.vo.mall.AdminPageRefundOrderVO;
import com.sc.nft.entity.vo.mall.AdminRefundDetailsVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.MallOrderRefundService;
import com.sc.nft.service.MallOrderService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/mall/order")
@Api(tags = "IP商城订单模块")
public class IPMallAdminOrderController {


    private final MallOrderService mallOrderService;
    private final Redisson redisson;
    private final MallOrderStatusLogDao mallOrderStatusLogDao;
    private final MallOrderRefundService mallOrderRefundService;

    @ApiOperation(value = "营销和兑换订单列表", notes = "营销和兑换订单列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pageOrder", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminMallPageOrderVO> adminPageOrder(@RequestBody AdminMallPageOrderRequest request) {
        return PageResult.success(mallOrderService.adminPageOrder(request.getActivityType(),request.getOrderNo(),request.getBeginTime(),request.getEndTime(),request.getPayBeginTime(), request.getPayEndTime(),request.getProductName(),request.getStatus(),request.getPayType(),request.getIsRefund(),request.getUserTel(),request.getPageNo(),request.getPageSize(),request.getSupplierId(),request.getCategoryName(),request.getOrderSource()));
    }

    @ApiOperation(value = "营销和兑换订单列表", notes = "营销和兑换订单列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getOrderDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<AdminOrderDetailsVO> adminOrderDetails(@RequestBody IdRequest request) {
        return Result.success(mallOrderService.adminOrderDetails(request.getId()));
    }

    @ApiOperation(value = "售后列表", notes = "售后列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pageRefundOrder", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    @Deprecated
    public PageResult<AdminPageRefundOrderVO> adminPageRefundOrder(@RequestBody RefundOrderRequest request) {
        return PageResult.success(mallOrderService.adminPageRefundOrder(request.getOrderNo(),request.getUserTel(),request.getRefundStatus(),request.getPageNo(),request.getPageSize(),request.getSupplierId()));
    }


    @ApiOperation(value = "售后详情", notes = "售后详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/refundDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    @Deprecated
    public Result<AdminRefundDetailsVO> adminRefundDetails(@RequestBody IdRequest request) {
        return Result.success(mallOrderService.adminRefundDetails(request.getId()));
    }


    @ApiOperation(value = "编辑商城订单备注", notes = "编辑商城订单备注", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/editAdminNote", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result editAdminNote(@RequestBody EditAdminNoteRequest request) {
        mallOrderService.editAdminNote(request.getId(),request.getAdminNote());
        return Result.success();
    }

    @ApiOperation(value = "导出订单", notes = "导出订单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/exportOrderList", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<ExportOrderListVO>> exportOrderList(@RequestBody ExportOrderListRequest request) {
        return Result.success(mallOrderService.exportOrderList(request.getType()));
    }


    @ApiOperation(value = "批量发货", notes = "批量发货", httpMethod = "POST")
    @PostMapping(value = "/batchDeliver")
//    @SysMemberLogin
    @ResponseBody
    public Result<List<BatchDeliverDTO>> batchDeliver(@RequestParam(value="file",required=false) MultipartFile file) {
        // 获取file文件以后,解析
        if (BeanUtil.isEmpty(file)) {
            return Result.fail("文件不能为空");
        }
        List<BatchDeliverDTO> list = new ArrayList<>();

        String key = RedisKeyEnum.BATCH_DELIVER_LOCK.getKey();
        RLock lock = redisson.getLock(key);
        try {
            boolean res = lock.tryLock(10, TimeUnit.SECONDS);
            if (res) {
                list = mallOrderService.batchDeliver(file);
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (IOException e) {
            throw new GlobalRunTimeException("文件解析失败，请重试");
        } catch (InterruptedException e) {
            throw new GlobalRunTimeException("上传失败，请重试");
        } finally {
            lock.unlock();
        }

        return Result.success(list);
    }

    @ApiOperation(value = "分销订单", notes = "分销订单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/distributionOrderPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<DistributionOrderVO> distributionOrderPage(@RequestBody DistributionOrderAdminReq distributionOrderAdminReq) {
        return PageResult.success(mallOrderService.getDistributionOrderVOAdminPage(distributionOrderAdminReq.getOrderNO(),distributionOrderAdminReq.getStartTime(),distributionOrderAdminReq.getEndTime(),distributionOrderAdminReq.getMiniUserTel(),distributionOrderAdminReq.getStatus(),
                distributionOrderAdminReq.getIncomeStatus(),distributionOrderAdminReq.getPageNo(),distributionOrderAdminReq.getPageSize()));
    }
}
